package jp.co.toshiba.ffsa.action.csde;

import java.util.ArrayList;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.common.Crypto;
import jp.co.toshiba.ffsa.dto.LoginDto;
import jp.co.toshiba.ffsa.form.csde.Csde601Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.csde.Csde601Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * 設計I/F - Submit確認画面
 * @author npc
 */
public class Csde601Action extends CommonAction {

    /** 画面ID */
    public String screenID = "CSDE601";

    /** アクションフォーム */
    @ActionForm
    @Resource
    protected Csde601Form csde601Form;

    /** アクションサービス */
    @ActionForm
    @Resource
    protected Csde601Service csde601Service;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String index() throws Exception {

        try {
            // ログイン済判定する
            if ( !judgeLogin(csde601Form.getSessionId()) ) {
                return topTransition;
            }

            // リファラ判定
            if ( !judgeRefferer() ) {
                return topTransition;
            }

            return "/csde/csde601.jsp";

        } catch (Exception e) {
            errorLogWriter(e, "index", screenID);
            throw e;
        }

    }

    /**
     * Submit処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = true,input="/csde/csde601/error")
    public String submit() throws Exception {

        String transition = "/csde/csde601";
        String result = ConstantValue.fail;

        try {
            // ログイン済判定する
            if( !judgeLogin(csde601Form.getSessionId()) ){
                return topTransition;
            }

            // リファラ判定
            if( !judgeRefferer() ){
                return topTransition;
            }

            // 暗号化されていた機密情報を復号
            decryptSecretData();

            // Serviceクラスを呼び出す
            csde601Service = new Csde601Service();
            csde601Service.setUserId(((LoginDto)RequestUtil.getRequest().getSession().getAttribute("login_dto")).getUserID());
            csde601Service.projectId    = csde601Form.projectId;
            csde601Service.projectName  = csde601Form.projectName;
            csde601Service.workflowId   = csde601Form.workflowId;
            csde601Service.workflowName = csde601Form.workflowName;
            csde601Service.revision     = csde601Form.revision;

            // FTPクライアントにメタデータをアップロード(暗号化）
            if( !csde601Service.writeMetadata() ){
                ActionMessages messages = new ActionMessages();
                messages.add("ftpUploadEncryptMetadata", new ActionMessage(csde601Service.getErrorCode(), true));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);

                // 機密情報を暗号化
                encryptSecretData();

                return transition;
            }

            //  ユーザへメール送信
            if( !csde601Service.mailSendToUser() ){
                ActionMessages messages = new ActionMessages();
                messages.add("failedMailSendToUser", new ActionMessage(csde601Service.getErrorCode(), true));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);

                // 機密情報を暗号化
                encryptSecretData();

                return transition;
            }

            //  管理者へメール送信
            if( !csde601Service.mailSendToManager() ){
                ActionMessages messages = new ActionMessages();
                messages.add("failedMailSendToManager", new ActionMessage(csde601Service.getErrorCode(), true));
                ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);

                // 機密情報を暗号化
                encryptSecretData();

                return transition;
            }

            result = ConstantValue.success;
            transition = "/csde/csde602.jsp";

            // 操作ログ記述
            if(ConstantValue.success.equals(result)){
                ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                ArrayList<String> parameterNames = new ArrayList<String>();
                ArrayList<String> values = new ArrayList<String>();
                parameterNames.add(MessageResourcesUtil.getMessage("csde601.003"));
                values.add(csde601Form.workflowId);
                parameterNames.add(MessageResourcesUtil.getMessage("csde601.004"));
                values.add(csde601Form.revision);
                parameters.add(parameterNames);
                parameters.add(values);
                entryOperationHistory(screenID, "submit", parameters, result);
            }

            // 機密情報を暗号化
            encryptSecretData();

        } catch (Exception e) {
            errorLogWriter(e, "submit", screenID);
            throw e;
        }
        return transition;

    }

    /**
     * バリデーションエラー処理
     * @return 遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String error() throws Exception {

        try {
            // ログイン済判定する
            if( !judgeLogin(csde601Form.getSessionId()) ){
                return topTransition;
            }

            // リファラ判定
            if( !judgeRefferer() ){
                return topTransition;
            }

            // セッションタイムアウト判定
            if( !judgeSession() ){
                return topTransition;
            }

            return index();

        }catch(Exception e) {
            errorLogWriter(e, "submit", screenID);
            throw e;
        }

    }

    private void encryptSecretData() throws Exception {
        csde601Form.projectId = Crypto.encrypt(csde601Form.projectId);
    }

    private void decryptSecretData() throws Exception {
        csde601Form.projectId = Crypto.decrypt(csde601Form.projectId);
    }


}